package com.ktwlrj.core.util.xpoi;

import com.ktwlrj.core.constant.ResultCode;
import com.ktwlrj.core.exception.ActionException;
import com.ktwlrj.core.util.xpoi.model.DataSourceField;
import com.ktwlrj.core.util.xpoi.model.ExcelParseData;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 单工作簿excel解析
 *
 * @author zhouxuanhong
 * @date 2019-08-05 22:23
 * @since JDK1.8
 */
public class SimpleWorkbookParse extends BaseWorkbook {
    public SimpleWorkbookParse() {
        this.excelParseData = new ExcelParseData();
    }

    @Override
    public void process() throws ActionException {
        boolean b = WorkbookUtils.compareToTemplate(workBook, parseWorkBook);
        if (!b) {
            throw new ActionException(ResultCode.VERIFICATION_FAILED.getValue(), "excel导入内容与模板定义不匹配");
        }
        // 获取第一张工作簿
        Sheet sheet = this.getSheet(0);

        // 工作簿自定义字段集
        Map<String, DataSourceField> cdsField = new HashMap<>(16);

        // 工作簿数据源字段集
        Map<String, DataSourceField> dsField = new HashMap<>(16);

        // 读取工作簿自定义字段，数据源字段数据集
        this.getDataSourceField(sheet, cdsField, dsField);
        sheet = this.getParseSheet(0);
        // 解析自定义字段
        if (!CollectionUtils.isEmpty(cdsField)) {
            this.parseCustomField(sheet, cdsField, excelParseData);
        }

        // 解析数据源
        if (!CollectionUtils.isEmpty(dsField)) {
            this.parseDataSource(sheet, dsField, dataStartRow, excelParseData);
        }
    }
}
